import box/{logger,condit}

# Powered By NerdFont

# 宽度
# Mono    | 等宽
# Propo   | 比例

# 变体
# Italic  | 斜体
# Bold    | 粗体
# Regular | 正常

if ! Has-File $NERDFONT_CFG;then
  Err "找不到配置文件"
  exit
elif ! . $NERDFONT_CFG;then
  Err "配置文件数据错误"
  exit
fi

lang=zh
title="Nerd Fonts"
font=$HOME/.termux/font.ttf

nf_choose(){
  CHOOSE=$(
    eval "
      dialog \
        --title '$title' \
        --menu 'Choose Font 选择字体' 20 40 1 \
        $(
          for ((i=0;i<${#Fonts[@]};i++));do
            echo -n $[i+1] "'${Fonts[i]}' "
          done
        ) \
        0 'return 返回' \
      2>&1 >/dev/tty        
    "
  )
  case ${CHOOSE:-0} in
    0) nf_main
    ;;
    *) dl=${Fonts[CHOOSE-1]}
       nf_choose_yesno
  esac
}

nf_choose_yesno(){  
  dialog \
    --title "$title" \
    --yesno \
    "Do you want to download $dl font?\n是否下载 $dl 字体？" \
    7 45
  RETURN=$?
  case $RETURN in
    0) nf_choose_download
    ;;
    1) nf_choose
  esac
}

nf_choose_download(){
  clear
  echo 'Waiting to download files from GitHub...'
  File $dl.zip && rm $dl.zip
  if \
    wget \
      -q \
      --show-progress \
      --read-timeout=10 \
      "$url/latest/download/$dl.zip"
  then
    dialog \
      --title "$title" \
      --msgbox \
    "$dl download done\n$dl 下载完成" \
    7 30
    nf_choose_unzip
  else
    dialog \
      --title "$title" \
      --msgbox \
    "$dl download fail\n$dl 下载失败" \
    7 30
    nf_choose
  fi
}

nf_choose_unzip(){
  dl_fonts=($(unzip -l $dl.zip *Mono-*.ttf | awk '{print $4}' | sed '/^$/d' | tail +3))
  CHOOSE=$(
    eval "
      dialog \
        --title '$title' \
        --menu 'Choose Fonts 选择字体' 21 50 1 \
        $(
          for ((i=0;i<${#dl_fonts[@]};i++));do
            echo -n $[i+1] "'${dl_fonts[i]}' "
          done
        ) \
        0 'cancel 取消' \
        2>&1 >/dev/tty
    "
  )
  case ${CHOOSE:-0} in
    0) Has-File $dl.zip && rm $dl.zip
    ;;
    *) Has-File $font && rm $font
       unzip $dl.zip ${dl_fonts[CHOOSE-1]} -d $(dirname $font)
       cd $(dirname $font)
       mv ${dl_fonts[CHOOSE-1]} $(basename $font)
       Has-File $dl.zip && rm $dl.zip
       termux-reload-settings
  esac
  nf_choose
}

nf_remove(){
  rm -f $font
  termux-reload-settings
  nf_main
}

nf_list(){
  COLS=$(tput cols)
  for ((i=0;i<${#Fonts[@]};i++));do
    printf "%*s\n" $COLS | tr ' ' '-'
    echo -e "\e[1;33m${Fonts[i]}: \e[1;36m$(eval "echo \${INFO_$lang[i]}")\n"
  done | less -R
  nf_main
}

nf_lang(){
  CHOOSE=$(
    dialog \
      --title "$title" \
      --menu "Choose README language 选择描述语言" 20 40 1 \
      1 "English 英文" \
      2 "Chinese 简体中文" \
      0 "return 返回" \
    2>&1 >/dev/tty
  )
  case ${CHOOSE:-0} in
    1) lang=en
       nf_main
    ;;
    2) lang=zh
       nf_main
    ;;
    0) nf_main
  esac
}

nf_main(){
  CHOOSE=$(
    dialog \
      --title "$title" \
      --menu "Main Menu 主菜单" 15 50 1 \
      1 "choose fonts 选择字体" \
      2 "remove current font 删除当前字体" \
      3 "fonts readme 字体描述" \
      4 "readme language 描述语言" \
      0 "exit 退出" \
    2>&1 >/dev/tty
  )
  case ${CHOOSE:-0} in
    1) nf_choose
    ;;
    2) nf_remove
    ;;
    3) nf_list
    ;;
    4) nf_lang
    ;;
    0) clear
       exit
    ;;
    *)
  esac  
}

nf_main